import { defHttp } from '/@/utils/http/axios';
import { LoginParams, LoginResultModel, GetUserInfoModel, User } from './model/userModel';

import { ErrorMessageMode } from '/#/axios';

enum Api {
  // Login = '/login',
  Login = '/api/user/login',
  Logout = '/api/user/logout',
  // GetUserInfo = '/getUserInfo',
  GetUserInfo = '/api/user/info',
  GetPermCode = '/api/permission/get_permission_code',
  GetUserList = '/api/user',
  AddUser = '/api/user',
  GetUserDetail = '/api/user/<pk>',
  UpdateUser = '/api/user/<pk>',
  DeleteUser = '/api/user/<pk>',
  AssignRole = '/api/user/<pk>/assign_role',
}

/**
 * @description: user login api
 */
export function loginApi(params: LoginParams, mode: ErrorMessageMode = 'modal') {
  return defHttp.post<LoginResultModel>(
    {
      url: Api.Login,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
}

/**
 * @description: getUserInfo
 */
export function getUserInfo() {
  return defHttp.get<GetUserInfoModel>({ url: Api.GetUserInfo }, { errorMessageMode: 'none' });
}

export function getPermCode() {
  return defHttp.get<string[]>({ url: Api.GetPermCode });
}

export function doLogout() {
  return defHttp.post({ url: Api.Logout });
}

export function getUserList(params: User) {
  return defHttp.get<User[]>(
    {
      url: Api.GetUserList,
      params,
    },
    {
      withToken: true,
      errorMessageMode: 'modal',
    },
  );
}

export function getUserDetail(pk: number) {
  return defHttp.get<User>(
    {
      url: Api.GetUserDetail.replace('<pk>', String(pk)),
    },
    {
      withToken: true,
      errorMessageMode: 'modal',
    },
  );
}

export function addUser(data: User) {
  return defHttp.post<User>(
    {
      url: Api.AddUser,
      data,
    },
    {
      withToken: true,
      errorMessageMode: 'modal',
    },
  );
}

export function updateUser(data: User) {
  return defHttp.put<User>(
    {
      url: Api.UpdateUser.replace('<pk>', String(data.id)),
      data,
    },
    {
      withToken: true,
      errorMessageMode: 'modal',
    },
  );
}

export function deleteUser(pk: number) {
  return defHttp.post<User>(
    {
      url: Api.DeleteUser.replace('<pk>', String(pk)),
    },
    {
      withToken: true,
      errorMessageMode: 'modal',
    },
  );
}

export function getUserRole(pk: number) {
  return defHttp.get(
    {
      url: Api.AssignRole.replace('<pk>', String(pk)),
    },
    {
      withToken: true,
      errorMessageMode: 'modal',
    },
  );
}

export function assignRole(pk: number, data) {
  return defHttp.put(
    {
      url: Api.AssignRole.replace('<pk>', String(pk)),
      data,
    },
    {
      withToken: true,
      errorMessageMode: 'modal',
    },
  );
}
